<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline">
          <el-input v-model="selectData.NameLike" placeholder="输入名称"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">类别</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.TypeEquals" placeholder="请选择类别">
            <el-option v-for="item in UnttypeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">统计地址</label>
        <div class="layui-input-inline">
          <el-cascader :options="UntyuseroupOptions" v-model="carryArea" filterable :show-all-levels="false"
            change-on-select>
          </el-cascader>
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :disabled="downloadLoading" :loading="this.$store.state.isloading">搜索
      </el-button>
      <el-button :disabled="this.$store.state.isloading || downloadLoading" @click="btnRepeat">重置</el-button>
    </div>
    <div>
      <ul class="toolList">
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon"
          @click="operate(item.BTNVALUE)">
          <el-button type="primary" :disabled="$store.state.isloading || downloadLoading" :value="item.BTNSHOWNAME"
            v-text="item.BTNSHOWNAME"></el-button>
        </li>
        <li>
          <el-button type="primary" @click="exportExcel" :disabled="this.$store.state.isloading">导出Excel</el-button>
        </li>
      </ul>
    </div>
    <dataTable :height="height" :headList="headList" :isIndex="true" :dataList="dataList" :pageOrder="pageOrder"
      @pageChange="pageChange" @selectRow="selectRow" :hiddenPageOrder="false" :hiddenColumn="hiddenColumn"></dataTable>
    <printmodal ref="printPdf"></printmodal>
    <!-- 操作弹窗 -->
    <main-model ref="dialog" @refreshData="getData"></main-model>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";

  .el-button+.el-button {
    margin-left: 0;
  }

  .btn {
    text-align: center;
  }

</style>
<script>
  import dataTable from "@/components/Table/DataTable";
  import printmodal from "@/components/Pdf/Print";
  import apiBase from "@/api/apiBase.js";
  import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  import {
    PRINT
  } from "@/components/Pdf/PDF.js";
  import mainModel from "./unitManagePopover";
  const EXPORT = "打印";
  export default {
    name: "unitManage",
    mixins: [manage],
    data() {
      return {
        selectData: {},
        downloadLoading: false,
        OutDataExcel: [],
        message: null,
        height: "620",
        sweetModalWidth: 80,
        sweetModalMaxWidth: 1000,
        hiddenColumn: true,
        carryArea: [],
        UnttypeOptions: [],
        UntyuseroupOptions: [],
      };
    },
    methods: {
      exportExcel() {
        var startTime;
        var endTime;
        if (this.selectData.CdhactualdateGreaterAndEquals) {
          startTime = this.selectData.CdhactualdateGreaterAndEquals.toLocaleDateString();
        } else {
          startTime = '空';
        }
        if (this.selectData.CdhactualdateLessAndEquals) {
          endTime = this.selectData.CdhactualdateLessAndEquals.toLocaleDateString();
        } else {
          endTime = '空';
        }
        var header = '单位统计表' + '(' + startTime + '--' + endTime + ')';
        if (this.dataList.length === 0) {
          this.$message.error("无导出数据！");
          return;
        }
        this.downloadLoading = true;
        console.log(this.headList);
        console.log(this.OutDataExcel);
        require.ensure([], () => {
          const {
            export_json_to_excel
          } = require("@/vendor/Export2Excel");
          export_json_to_excel(this.headList, this.OutDataExcel, header);
          this.downloadLoading = false;
        });
      },
      //功能操作
      operate(order) {
        this.order = order;
        switch (this.order) {
          case EXPORT:
            this.$refs.printPdf.report(PRINT.PDFDRIVERS, this.getDataC());
            break;
          case '新增':
            this.$refs.dialog.open(this.order);
            break;
          case '修改':
            this.$refs.dialog.open(this.order, this.currentRow);
            break;
          case '详情':
            break;
          case '删除':
            break;
          default:
        }
      },
      getColumns() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Column.GetUnitColumns,
          res => {
            this.headList = res.data.content;
          }
        );
      },
      getDataC() {
        let Data = JSON.parse(JSON.stringify(this.selectData));
        if (Data.RegiskeypointEquals === null || Data.RegiskeypointEquals === undefined) Data.RegiskeypointEquals = 'A';
        Data.CdhactualdateGreaterAndEquals = formatDate(
          Data.CdhactualdateGreaterAndEquals,
          "yyyyMMdd"
        );
        Data.CdhactualdateLessAndEquals = formatDate(
          Data.CdhactualdateLessAndEquals,
          "yyyyMMdd"
        );
        let data = {
          PageIndex: 1,
          PageSize: 100,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        return data;
      },
      getData() {
        let Data = JSON.parse(JSON.stringify(this.selectData));
        Data.UsergroupEquals = this.carryArea[this.carryArea.length - 1];
        let data = {
          PageIndex: 1,
          PageSize: 10,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Unit.Query,
          res => {
            // this.hiddenColumnInfo = true;
            this.dataList = res.data.content.OutData;
            this.OutDataExcel = res.data.content.OutData;
            this.pageOrder.totalNum = res.data.content.RecordCount;
          }
        );

      },

      //当前选中行
      selectRow(row, order) {
        this.currentRow = row;
        this.$emit("selectRow", row, "");
        this.$refs.dialog.open(order, this.currentRow);
      },
      // dataRepeat(){},
      btnRepeat() {
        this.selectData = {};
        this.getData();
      },
      btnShow(btn) {
        switch (btn) {
          case EXPORT:
            return true;
            break;
          case '新增':
            return true;
            break;
          case '修改':
            return true;
            break;
          case '详情':
            return true;
            break;
          case '删除':
            return true;
            break;
          default:
            return false;
        }
      },
    },
    mounted() {},
    components: {
      dataTable,
      mainModel,
      printmodal
    },

    created() {
      // this.getColumns();
      // this.getData(); 
    this.headList = [{
        "ColumnKey": "UNTNAME",
        "ColumnName": "单位名称",
        "ColumnOrder": "UNTNAME",
        "ColumnAlign": "center",
        "ColumnWidth": 150
      }, {
        "ColumnKey": "UNTBUILDDATE",
        "ColumnName": "建设时间",
        "ColumnOrder": "UNTBUILDDATE",
        "ColumnAlign": "center",
        "ColumnWidth": 150
      }, {
        "ColumnKey": "ADDNAME",
        "ColumnName": "统计地址",
        "ColumnOrder": "ADDNAME",
        "ColumnAlign": "center",
        "ColumnWidth": 120
      }, {
        "ColumnKey": "UNTTYPE",
        "ColumnName": "类别",
        "ColumnOrder": "UNTTYPE",
        "ColumnAlign": "center",
        "ColumnWidth": 120
      }, {
        "ColumnKey": "UNTGREENRATE",
        "ColumnName": "墓区绿化率",
        "ColumnOrder": "UNTGREENRATE",
        "ColumnAlign": "center",
        "ColumnWidth": 120
      }, {
        "ColumnKey": "UNTAPVTOTALAREA",
        "ColumnName": "审批总面积",
        "ColumnOrder": "UNTAPVTOTALAREA",
        "ColumnAlign": "center",
        "ColumnWidth": 120
      }, {
        "ColumnKey": "UNTLOCATION",
        "ColumnName": "地址",
        "ColumnOrder": "UNTLOCATION",
        "ColumnAlign": "center",
        "ColumnWidth": 200
      }, {
        "ColumnKey": "UNTSURPLUSNUM",
        "ColumnName": "剩余墓穴总量",
        "ColumnOrder": "UNTSURPLUSNUM",
        "ColumnAlign": "center",
        "ColumnWidth": 150
      }, {
        "ColumnKey": "UNTSTATUS",
        "ColumnName": "状态",
        "ColumnOrder": "UNTSTATUS",
        "ColumnAlign": "center",
        "ColumnWidth": 120
      }]
    this.dataList = [{
        "UNTID": "330481X1L1UNT20190506090518704",
        "UNTNAME": "1公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "45长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085945980",
        "UNTNAME": "2公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "经营性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "45长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085925808",
        "UNTNAME": "31公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "45长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085909777",
        "UNTNAME": "4公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "5长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085853152",
        "UNTNAME": "5公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "3长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085828090",
        "UNTNAME": "6公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "2长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085803824",
        "UNTNAME": "7公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "1长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085742871",
        "UNTNAME": "8公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "2长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085716199",
        "UNTNAME": "0公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "1长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }, {
        "UNTID": "330481X1L1UNT20190424085658387",
        "UNTNAME": "9公墓",
        "UNTLOCATION": "",
        "UNTTYPE": "开辟生态墓区的公益性公墓",
        "UNTSTATUS": "正常",
        "UNTBUILDDATE": "",
        "UNTGREENRATE": 0.0,
        "UNTAPVTOTALAREA": 0.0,
        "UNTSURPLUSNUM": 0,
        "ADDNAME": "0长安镇",
        "UNTONLINE": 0,
        "UNTONLINEDESC": "未联网"
      }
    ]
      var data = {
        type: '单位类别'
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Code.GetType,
        res => {
          let cardata = res.data.content;
          this.UnttypeOptions = cardata.map(item => ({
            value: item.CodeValue,
            label: item.CodeName,
          }));
        });
      apiBase.DoService({},
        apiBase.getApiAdress().Code.GetAddressMap,
        res => {
          let cardata = res.data.content;
          this.UntyuseroupOptions = cardata.map(item => ({
            value: item.ADDID,
            label: item.ADDNAME,
            ADDLEVEL: item.ADDLEVEL,
            ADDTOPID: item.ADDTOPID,
            children: item.Children.map(itemStr => ({
              value: itemStr.ADDID,
              label: itemStr.ADDNAME,
              ADDLEVEL: itemStr.ADDLEVEL,
              ADDTOPID: itemStr.ADDTOPID
            }))
          }));
        });
    }
  };

</script>
